package com.itboye.lottery.utils.notifycation;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.NotificationManagerCompat;

import com.itboye.lottery.app.MyApplication;

public class NotifycationCheck {
    public static boolean checkAndOpennotifycation(Activity activity) {
        boolean isOpen = isOpenNotifycation();
        if (!isOpen) {
            goToChangeNotifycationEnable(activity);
        }
        return isOpen;
    }

    public static void goToChangeNotifycationEnable(Activity activity){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            Intent intent = new Intent();
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("app_package", MyApplication.Companion.getInstance().getPackageName());
            intent.putExtra("app_uid", MyApplication.Companion.getInstance().getApplicationInfo().uid);
            activity.startActivity(intent);
        } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
            goAppDetail(activity);
        } else if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
            intent.putExtra(Settings.EXTRA_APP_PACKAGE, MyApplication.Companion.getInstance().getPackageName());
            activity.startActivity(intent);
        }
    }

    public static void goAppDetail(Activity activity) {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setData(Uri.parse("package:" + activity.getPackageName()));
        activity.startActivity(intent);
    }

    public static boolean isOpenNotifycation() {
        NotificationManagerCompat manager = NotificationManagerCompat.from(MyApplication.Companion.getInstance());
        return manager.areNotificationsEnabled();
    }
}
